{% extends 'stocks/base.html' %}

{% block title %}{{ stock.symbol }} - {{ stock.name }} - 股票详情{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h2>
        <i class="bi bi-building"></i> {{ stock.symbol }} - {{ stock.name }}
    </h2>
    <div class="btn-group">
        <a href="{% url 'daily_data' stock.symbol %}" class="btn btn-success">
            <i class="bi bi-bar-chart"></i> 交易数据
        </a>
        <a href="{% url 'financial_data' stock.symbol %}" class="btn btn-info">
            <i class="bi bi-calculator"></i> 财务指标
        </a>
        <a href="{% url 'stock_list' %}" class="btn btn-outline-secondary">
            <i class="bi bi-arrow-left"></i> 返回列表
        </a>
    </div>
</div>

<div class="row">
    <!-- 公司基本信息 -->
    <div class="col-md-6">
        <div class="card mb-4">
            <div class="card-header">
                <h5><i class="bi bi-info-circle"></i> 公司基本信息</h5>
            </div>
            <div class="card-body">
                {% if detail %}
                <div class="row">
                    <div class="col-md-6">
                        <p><strong>公司全称:</strong><br>{{ detail.org_name_cn }}</p>
                        <p><strong>公司简称:</strong><br>{{ detail.org_short_name_cn }}</p>
                        <p><strong>主营业务:</strong><br>{{ detail.main_operation_business|default:"暂无信息" }}</p>
                        <p><strong>法定代表人:</strong><br>{{ detail.legal_representative|default:"暂无信息" }}</p>
                        <p><strong>总经理:</strong><br>{{ detail.general_manager|default:"暂无信息" }}</p>
                    </div>
                    <div class="col-md-6">
                        <p><strong>董事长:</strong><br>{{ detail.chairman|default:"暂无信息" }}</p>
                        <p><strong>实际控制人:</strong><br>{{ detail.actual_controller|default:"暂无信息" }}</p>
                        <p><strong>所属省份:</strong><br>{{ detail.provincial_name|default:"暂无信息" }}</p>
                        <p><strong>员工人数:</strong><br>{{ detail.staff_num|default:"暂无信息" }}</p>
                        <p><strong>注册资本:</strong><br>
                            {% if detail.reg_asset %}
                                {{ detail.reg_asset|floatformat:2 }} 万元
                            {% else %}
                                暂无信息
                            {% endif %}
                        </p>
                    </div>
                </div>
                {% else %}
                <p class="text-center text-muted">暂无详细公司信息</p>
                {% endif %}
            </div>
        </div>
    </div>

    <!-- 联系信息 -->
    <div class="col-md-6">
        <div class="card mb-4">
            <div class="card-header">
                <h5><i class="bi bi-telephone"></i> 联系信息</h5>
            </div>
            <div class="card-body">
                {% if detail %}
                <div class="row">
                    <div class="col-md-6">
                        <p><strong>电话:</strong><br>{{ detail.telephone|default:"暂无信息" }}</p>
                        <p><strong>邮箱:</strong><br>{{ detail.email|default:"暂无信息" }}</p>
                        <p><strong>网站:</strong><br>
                            {% if detail.org_website %}
                                <a href="{{ detail.org_website }}" target="_blank">{{ detail.org_website }}</a>
                            {% else %}
                                暂无信息
                            {% endif %}
                        </p>
                    </div>
                    <div class="col-md-6">
                        <p><strong>注册地址:</strong><br>{{ detail.reg_address_cn|default:"暂无信息" }}</p>
                        <p><strong>办公地址:</strong><br>{{ detail.office_address_cn|default:"暂无信息" }}</p>
                        <p><strong>成立日期:</strong><br>
                            {% if detail.established_date %}
                                {{ detail.established_date }}
                            {% else %}
                                暂无信息
                            {% endif %}
                        </p>
                        <p><strong>上市日期:</strong><br>
                            {% if detail.listed_date %}
                                {{ detail.listed_date }}
                            {% else %}
                                暂无信息
                            {% endif %}
                        </p>
                    </div>
                </div>
                {% else %}
                <p class="text-center text-muted">暂无联系信息</p>
                {% endif %}
            </div>
        </div>
    </div>
</div>

<!-- 股东信息 -->
<div class="row">
    <div class="col-12">
        <div class="card mb-4">
            <div class="card-header">
                <h5><i class="bi bi-people"></i> 最新股东信息</h5>
            </div>
            <div class="card-body">
                {% if shareholder_data %}
                <div class="table-responsive">
                    <table class="table table-sm table-hover">
                        <thead class="table-light">
                            <tr>
                                <th>报告期</th>
                                <th>股东名称</th>
                                <th>股东类型</th>
                                <th>持股数量</th>
                                <th>持股比例</th>
                                <th>变动类型</th>
                                <th>变动数量</th>
                                <th>变动比例</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for data in shareholder_data %}
                            <tr>
                                <td>{{ data.date|date:"Y-m-d" }}</td>
                                <td>{{ data.shareholder_name|default:"-" }}</td>
                                <td>{{ data.shareholder_type|default:"-" }}</td>
                                <td>
                                    {% if data.share_number %}
                                        {{ data.share_number|floatformat:0 }}
                                    {% else %}
                                        -
                                    {% endif %}
                                </td>
                                <td>
                                    {% if data.share_ratio %}
                                        {{ data.share_ratio|floatformat:4 }}%
                                    {% else %}
                                        -
                                    {% endif %}
                                </td>
                                <td>
                                    {% if data.change_type == "增加" %}
                                        <span class="text-success">{{ data.change_type }}</span>
                                    {% elif data.change_type == "减少" %}
                                        <span class="text-danger">{{ data.change_type }}</span>
                                    {% else %}
                                        {{ data.change_type|default:"-" }}
                                    {% endif %}
                                </td>
                                <td>
                                    {% if data.change_number %}
                                        {{ data.change_number|floatformat:0 }}
                                    {% else %}
                                        -
                                    {% endif %}
                                </td>
                                <td>
                                    {% if data.change_ratio %}
                                        {{ data.change_ratio|floatformat:4 }}%
                                    {% else %}
                                        -
                                    {% endif %}
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
                {% else %}
                <p class="text-center text-muted">暂无股东信息</p>
                {% endif %}
            </div>
        </div>
    </div>
</div>

<!-- 最近交易数据 -->
<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5><i class="bi bi-bar-chart"></i> 最近30天交易数据</h5>
                <a href="{% url 'daily_data' stock.symbol %}" class="btn btn-sm btn-outline-primary">
                    查看全部 <i class="bi bi-arrow-right"></i>
                </a>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-sm table-hover">
                        <thead class="table-light">
                            <tr>
                                <th>日期</th>
                                <th>开盘价</th>
                                <th>收盘价</th>
                                <th>最高价</th>
                                <th>最低价</th>
                                <th>成交量</th>
                                <th>成交额</th>
                                <th>涨跌幅</th>
                                <th>换手率</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for data in daily_data %}
                            <tr>
                                <td>{{ data.trade_date }}</td>
                                <td>{{ data.open|default:"-" }}</td>
                                <td>{{ data.close|default:"-" }}</td>
                                <td>{{ data.high|default:"-" }}</td>
                                <td>{{ data.low|default:"-" }}</td>
                                <td>{{ data.volume|default:"-" }}</td>
                                <td>
                                    {% if data.amount %}
                                        {{ data.amount|floatformat:2 }}
                                    {% else %}
                                        -
                                    {% endif %}
                                </td>
                                <td class="{% if data.change_pct and data.change_pct > 0 %}positive-change{% elif data.change_pct and data.change_pct < 0 %}negative-change{% endif %}">
                                    {% if data.change_pct %}
                                        {{ data.change_pct|floatformat:2 }}%
                                    {% else %}
                                        -
                                    {% endif %}
                                </td>
                                <td>
                                    {% if data.turnover_rate %}
                                        {{ data.turnover_rate|floatformat:2 }}%
                                    {% else %}
                                        -
                                    {% endif %}
                                </td>
                            </tr>
                            {% empty %}
                            <tr>
                                <td colspan="9" class="text-center text-muted py-3">暂无交易数据</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
